package com.trkj.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trkj.dao.MemberDAO;
import com.trkj.dao.MemberDAOFactory;

/**
 * 处理注册的请求
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MemberDAO memberDAO 
		= MemberDAOFactory.createDAO();

	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		String loginName = request.getParameter("login_name");
		String password = request.getParameter("password");
		
		boolean v = memberDAO.register(loginName, password);
		
		String error = "对不起，注册失败！";
		//如果注册成功，则跳转到登陆页面
		//如果注册失败，则继续注册
		String url = v ? "login.jsp" : "reg.jsp";
		request.setAttribute("error", error);
		if(v){
			response.sendRedirect(url);
		}else{
			request.getRequestDispatcher(url).forward(request, response);
		}
	}

}
